/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wss.module.market.bean;

import ohos.data.rdb.Statement;
import ohos.data.resultset.ResultSet;
import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.internal.DaoConfig;

public class GoodsInfoDao extends AbstractDao<GoodsInfo, String> {

    /**
     * TABLENAME
     */
    public static final String TABLENAME = "NOTE";

    /**
     * one
     */
    public static final int ONE = 1;

    /**
     * two
     */
    public static final int TWO = 2;

    /**
     * three
     */
    public static final int THREE = 3;

    /**
     * four
     */
    public static final int FOUR = 4;

    /**
     * five
     */
    public static final int FIVE = 5;

    /**
     * Six
     */
    public static final int SIX = 6;

    /**
     * Seven
     */
    public static final int SEVEN = 7;

    /**
     * Eight
     */
    public static final int EIGHT = 8;

    /**
     * Nine
     */
    public static final int NINE = 9;

    public static class Properties {
        /**
         * goodsId
         */
        public static final Property ID = new Property(0, String.class, "goodsId", true, "_id");

        /**
         * goodsName
         */
        public static final Property GOODS_NAME = new Property(1, String.class, "goodsName", false, "GOODS_NAME");

        /**
         * goodsPrice
         */
        public static final Property GOODS_PRICE = new Property(2, String.class, "goodsPrice", false, "GOODS_PRICE");

        /**
         * goodsOldPrice
         */
        public static final Property GOODS_OLD_PRICE = new Property(3, String.class, "goodsOldPrice", false, "GOODS_OLD_PRICE");

        /**
         * goodsMasterImg
         */
        public static final Property GOODS_MASTER_IMG = new Property(4, String.class, "goodsMasterImg", false, "GOODS_MASTER_IMG");

        /**
         * praiseRate
         */
        public static final Property PRAISE_RATE = new Property(5, String.class, "praiseRate", false, "PRAISE_RATE");

        /**
         * commentCount
         */
        public static final Property COMMENT_COUNT = new Property(6, String.class, "commentCount", false, "COMMENT_COUNT");

        /**
         * num
         */
        public static final Property NUM = new Property(7, Integer.class, "num", false, "NUM");

        /**
         * vendorId
         */
        public static final Property VENDOR_ID = new Property(8, String.class, "vendorId", false, "VENDOR_ID");
    }

    public GoodsInfoDao(DaoConfig config) {
        super(config);
    }

    public GoodsInfoDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    public static void createTable(Database db, boolean isExists) {
        String constraint = isExists ? "IF NOT EXISTS " : "";
        db.execSQL("CREATE TABLE " + constraint + "\"NOTE\" (" + //
                "\"_id\" TEXT PRIMARY KEY ," + // 0: goodsId
                "\"GOODS_NAME\" TEXT NOT NULL ," + // 1: goodsName
                "\"GOODS_PRICE\" TEXT," + // 2: goodsPrice
                "\"GOODS_OLD_PRICE\" TEXT," + // 3: goodsOldPrice
                "\"GOODS_MASTER_IMG\" TEXT," + // 4: goodsMasterImg
                "\"PRAISE_RATE\" TEXT," + // 5: praiseRate
                "\"COMMENT_COUNT\" TEXT," + // 6: commentCount
                "\"NUM\" INTEGER," + // 7: num
                "\"VENDOR_ID\" TEXT);"); // 8: vendorId
        // Add Indexes
        db.execSQL("CREATE UNIQUE INDEX " + constraint + "IDX_NOTE_TEXT_DATE_DESC ON \"NOTE\""
                + " (\"TEXT\" ASC,\"DATE\" DESC);");
    }

    /**
     * Drops the underlying database table.
     *
     * @param db database
     * @param isExists isExists
     */
    public static void dropTable(Database db, boolean isExists) {
        String sql = "DROP TABLE " + (isExists ? "IF EXISTS " : "") + "\"NOTE\"";
        db.execSQL(sql);
    }

    @Override
    protected GoodsInfo readEntity(ResultSet resultSet, int offset) {
        GoodsInfo entity = new GoodsInfo();
        entity.setGoodsId(resultSet.isColumnNull(offset + 0) ? null : resultSet.getString(offset + 0));
        entity.setGoodsName(resultSet.isColumnNull(offset + ONE) ? null : resultSet.getString(offset + ONE));
        entity.setGoodsPrice(resultSet.isColumnNull(offset + TWO) ? null : resultSet.getString(offset + TWO));
        entity.setGoodsOldPrice(resultSet.isColumnNull(offset + THREE) ? null : resultSet.getString(offset + THREE));
        entity.setGoodsMasterImg(resultSet.isColumnNull(offset + FOUR) ? null : resultSet.getString(offset + FOUR));
        entity.setPraiseRate(resultSet.isColumnNull(offset + FIVE) ? null : resultSet.getString(offset + FIVE));
        entity.setCommentCount(resultSet.isColumnNull(offset + SIX) ? null : resultSet.getString(offset + SIX));
        entity.setNum(resultSet.isColumnNull(offset + SEVEN) ? null : resultSet.getInt(offset + SEVEN));
        entity.setVendorId(resultSet.isColumnNull(offset + EIGHT) ? null : resultSet.getString(offset + EIGHT));
        return entity;
    }

    @Override
    protected String readKey(ResultSet cursor, int offset) {
        return cursor.isColumnNull(offset + 0) ? null : cursor.getString(offset + 0);
    }

    @Override
    protected void readEntity(ResultSet resultSet, GoodsInfo entity, int offset) {
        entity.setGoodsId(resultSet.isColumnNull(offset + 0) ? null : resultSet.getString(offset + 0));
        entity.setGoodsName(resultSet.isColumnNull(offset + ONE) ? null : resultSet.getString(offset + ONE));
        entity.setGoodsPrice(resultSet.isColumnNull(offset + TWO) ? null : resultSet.getString(offset + TWO));
        entity.setGoodsOldPrice(resultSet.isColumnNull(offset + THREE) ? null : resultSet.getString(offset + THREE));
        entity.setGoodsMasterImg(resultSet.isColumnNull(offset + FOUR) ? null : resultSet.getString(offset + FOUR));
        entity.setPraiseRate(resultSet.isColumnNull(offset + FIVE) ? null : resultSet.getString(offset + FIVE));
        entity.setCommentCount(resultSet.isColumnNull(offset + SIX) ? null : resultSet.getString(offset + SIX));
        entity.setNum(resultSet.isColumnNull(offset + SEVEN) ? null : resultSet.getInt(offset + SEVEN));
        entity.setVendorId(resultSet.isColumnNull(offset + EIGHT) ? null : resultSet.getString(offset + EIGHT));
    }

    @Override
    protected void bindValues(DatabaseStatement stmt, GoodsInfo entity) {
        stmt.clearBindings();
        String id = entity.getGoodsId();
        if (id != null) {
            stmt.bindString(ONE, id);
        }
        stmt.bindString(TWO, entity.getGoodsName());
        stmt.bindString(THREE, entity.getGoodsPrice());
        stmt.bindString(FOUR, entity.getGoodsOldPrice());
        stmt.bindString(FIVE, entity.getGoodsMasterImg());
        stmt.bindString(SIX, entity.getPraiseRate());
        stmt.bindString(SEVEN, entity.getCommentCount());
        stmt.bindLong(EIGHT, entity.getNum());
        stmt.bindString(NINE, entity.getVendorId());
    }

    @Override
    protected void bindValues(Statement stmt, GoodsInfo entity) {
        stmt.clearValues();
        String id = entity.getGoodsId();
        if (id != null) {
            stmt.setString(ONE, id);
        }
        stmt.setString(TWO, entity.getGoodsName());
        stmt.setString(THREE, entity.getGoodsPrice());
        stmt.setString(FOUR, entity.getGoodsOldPrice());
        stmt.setString(FIVE, entity.getGoodsMasterImg());
        stmt.setString(SIX, entity.getPraiseRate() == null ? "" : entity.getPraiseRate());
        stmt.setString(SEVEN, entity.getCommentCount() == null ? "" : entity.getCommentCount());
        stmt.setLong(EIGHT, entity.getNum());
        stmt.setString(NINE, entity.getVendorId());
    }

    @Override
    protected String updateKeyAfterInsert(GoodsInfo goodsInfo, long l) {

        return null;
    }

    @Override
    protected String getKey(GoodsInfo entity) {
        if (entity != null) {
            return entity.getGoodsId();
        } else {
            return null;
        }
    }

    @Override
    protected boolean hasKey(GoodsInfo entity) {
        return entity.getGoodsId() != null;
    }

    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }
}
